package com.gp.testonline.controller;

import com.gp.testonline.eo.QuestioninfoEOPage;
import com.gp.testonline.model.Questioninfo;
import com.gp.testonline.service.QuestioninfoService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "questioninfo")
@RestController
@CrossOrigin
public class QuestioninfoController {

    @Autowired
    private QuestioninfoService questioninfoService;

    @ApiOperation(value = "题库信息添加方法")
    @RequestMapping(value = "/insertQuestioninfo",method = RequestMethod.GET)
    public ResponseMessage insert(Questioninfo questioninfo){
        int IsInsert=0;
        //传入数据的判断
        //(1) questioninfo不能为空
        if (questioninfo!=null){
            //(2) 试题内容不能为空
            if (questioninfo.getQicontent()!=null){
                //(3) 添加试题
                IsInsert=questioninfoService.InsertQuestioninfo(questioninfo);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("试题内容重复不允许添加！");
                }else{
                    return Result.error("保存失败！");
                }
            }else {
                return Result.error("试题内容不能为空！");
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "题库信息修改方法")
    @RequestMapping(value = "/updateQuestioninfo",method = RequestMethod.GET)
    public ResponseMessage updateQuestioninfo(Questioninfo questioninfo){
        int isUpdate=0;
        //传入的参数不能为空
        if(questioninfo!=null){
            //参数当中主键不能为0
            if (questioninfo.getQiid()!=0){
                isUpdate=questioninfoService.UpdateQuestioninfo(questioninfo);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！");
                }
            }else {
                return Result.error("传入的参数不正确");
            }
        }
        return Result.error("传入的参数不正确");
    }
    @ApiOperation(value = "题库信息删除方法")
    @RequestMapping(value = "/deleteQuestioninfo",method = RequestMethod.GET)
    public ResponseMessage deleteQuestioninfo(int qiid){
        int isDelete=0;
        //传入的参数不能为空
        if(qiid!=0){
            //参数当中主键不能为0
            isDelete=questioninfoService.DeleteQuestioninfo(qiid);
            if (isDelete>0){
                return Result.success("删除成功！");
            }else {
                return Result.error("删除失败！");
            }
        }else{
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取题库详细信息")
    @RequestMapping(value = "/getQuestioninfo",produces = "application/json;charest=utf-8",method = RequestMethod.GET)
    public ResponseMessage getQuestioninfo(QuestioninfoEOPage questioninfoEOPage) {
//        Map<String, List> map = new HashMap<>();
        List<QuestioninfoEOPage> listQuestioninfo = questioninfoService.getQuestioninfoAll(questioninfoEOPage);
//        map.put("list",listQuestioninfo);
        return Result.success("200",listQuestioninfo);
    }
}
